﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Commonlib;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace AlbonWeb.Manager.StyleManager
{
    public partial class SelectRules : System.Web.UI.Page
    {
        protected readonly BLL.StyleBLL stylebll = new BLL.StyleBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDDL(ddlRef1, "--refStyle1--");
                bindDDL(ddlRef2, "--refStyle2--");
                bindRule();
            }
        }

        //绑定所有规则 
        private void bindRule()
        {
            ViewState["limit"] = " where 1=1";
            string cmdstr = "select count(*) from SelectRulesView"+ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","SelectRulesView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","createDate Desc")
            };
            SqlHelper.BindRepeater(repRule, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
        }
        protected void bindDDL(DropDownList ddl,string value)
        {
            string idlist = HttpUtility.UrlDecode(Commonlib.CookieHelper.GetCookieValue("ruleCart"));
            Commonlib.CookieHelper.DelCookies("ruleCart");
            string cmdstr = "select * from Style where ID in ("+idlist+")";
            SqlHelper.BindDropDownList(ddl, "styleName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
            ddl.Items.Insert(0, value);
        }

        //添加规则
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (ddlRef1.SelectedIndex == 0 || ddlRef2.SelectedIndex==0)
            {
                lbmessage.Text = "Pls select the style you want to add!";
            }
            else
            {
                lbmessage.Text =stylebll.addSelectRule(ddlRef1.SelectedValue, ddlRef2.SelectedValue, rbtnrule.SelectedValue);
            }
            bindRule();
        }
        //删除规则
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            BLL.StyleBLL stylebll = new BLL.StyleBLL();
            ImageButton imgDelete = (ImageButton)sender;
            stylebll.DeleteRule(imgDelete.CommandArgument);
            bindRule();
        }
        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindRule();
        }
    }
}